package com.alipay.api.internal.mapping;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayResponse;
import com.alipay.api.internal.mapping.AlipayFieldMethod;
import com.alipay.api.internal.mapping.ApiField;
import com.alipay.api.internal.mapping.ApiListField;
import com.alipay.api.internal.mapping.Reader;
import com.alipay.api.internal.util.StringUtils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

public class Converters {

   public static boolean isCheckJsonType = false;
   private static final Set baseFields = new HashSet();
   private static final Set excludeFields = new HashSet();
   private static final Set overideFields = new HashSet();


   public static Object convert(Class clazz, Reader reader) throws AlipayApiException {
      Object rsp = null;

      try {
         rsp = clazz.newInstance();
         BeanInfo e = Introspector.getBeanInfo(clazz);
         PropertyDescriptor[] pds = e.getPropertyDescriptors();
         boolean isResponseClazz = AlipayResponse.class.isAssignableFrom(clazz);
         PropertyDescriptor[] arr$ = pds;
         int len$ = pds.length;

         for(int i$ = 0; i$ < len$; ++i$) {
            PropertyDescriptor pd = arr$[i$];
            Method writeMethod = pd.getWriteMethod();
            if(writeMethod != null) {
               String itemName = pd.getName();
               String listName = null;
               if(!isResponseClazz || !excludeFields.contains(itemName)) {
                  ArrayList alipayFieldMethods = new ArrayList();
                  Field i$1;
                  AlipayFieldMethod alipayFieldMethod;
                  if(baseFields.contains(itemName) && isResponseClazz) {
                     i$1 = AlipayResponse.class.getDeclaredField(itemName);
                     alipayFieldMethod = new AlipayFieldMethod();
                     alipayFieldMethod.setField(i$1);
                     if(writeMethod.getDeclaringClass().getName().contains("AlipayResponse")) {
                        alipayFieldMethod.setMethod(writeMethod);
                     } else {
                        writeMethod = tryGetSetMethod(AlipayResponse.class, i$1, writeMethod.getName());
                        if(writeMethod == null) {
                           continue;
                        }

                        alipayFieldMethod.setMethod(writeMethod);
                     }

                     alipayFieldMethods.add(alipayFieldMethod);
                     if(overideFields.contains(itemName)) {
                        i$1 = tryGetFieldWithoutExp(clazz, itemName);
                        if(i$1 != null) {
                           writeMethod = tryGetSetMethod(clazz, i$1, writeMethod.getName());
                           if(writeMethod == null) {
                              continue;
                           }

                           alipayFieldMethod = new AlipayFieldMethod();
                           alipayFieldMethod.setField(i$1);
                           alipayFieldMethod.setMethod(writeMethod);
                           alipayFieldMethods.add(alipayFieldMethod);
                        }
                     }
                  } else {
                     i$1 = clazz.getDeclaredField(itemName);
                     alipayFieldMethod = new AlipayFieldMethod();
                     alipayFieldMethod.setField(i$1);
                     alipayFieldMethod.setMethod(writeMethod);
                     alipayFieldMethods.add(alipayFieldMethod);
                  }

                  Iterator var27 = alipayFieldMethods.iterator();

                  while(var27.hasNext()) {
                     alipayFieldMethod = (AlipayFieldMethod)var27.next();
                     Field field = alipayFieldMethod.getField();
                     Method method = alipayFieldMethod.getMethod();
                     ApiField jsonField = (ApiField)field.getAnnotation(ApiField.class);
                     if(jsonField != null) {
                        itemName = jsonField.value();
                     }

                     ApiListField jsonListField = (ApiListField)field.getAnnotation(ApiListField.class);
                     if(jsonListField != null) {
                        listName = jsonListField.value();
                     }

                     if(reader.hasReturnField(itemName) || listName != null && reader.hasReturnField(listName)) {
                        Class typeClass = field.getType();
                        Object obj;
                        if(String.class.isAssignableFrom(typeClass)) {
                           obj = reader.getPrimitiveObject(itemName);
                           if(obj instanceof String) {
                              method.invoke(rsp, new Object[]{obj.toString()});
                           } else {
                              if(isCheckJsonType && obj != null) {
                                 throw new AlipayApiException(itemName + " is not a String");
                              }

                              if(obj != null) {
                                 method.invoke(rsp, new Object[]{obj.toString()});
                              } else {
                                 method.invoke(rsp, new Object[]{""});
                              }
                           }
                        } else if(Long.class.isAssignableFrom(typeClass)) {
                           obj = reader.getPrimitiveObject(itemName);
                           if(obj instanceof Long) {
                              method.invoke(rsp, new Object[]{(Long)obj});
                           } else {
                              if(isCheckJsonType && obj != null) {
                                 throw new AlipayApiException(itemName + " is not a Number(Long)");
                              }

                              if(StringUtils.isNumeric(obj)) {
                                 method.invoke(rsp, new Object[]{Long.valueOf(obj.toString())});
                              }
                           }
                        } else if(Integer.class.isAssignableFrom(typeClass)) {
                           obj = reader.getPrimitiveObject(itemName);
                           if(obj instanceof Integer) {
                              method.invoke(rsp, new Object[]{(Integer)obj});
                           } else {
                              if(isCheckJsonType && obj != null) {
                                 throw new AlipayApiException(itemName + " is not a Number(Integer)");
                              }

                              if(StringUtils.isNumeric(obj)) {
                                 method.invoke(rsp, new Object[]{Integer.valueOf(obj.toString())});
                              }
                           }
                        } else if(Boolean.class.isAssignableFrom(typeClass)) {
                           obj = reader.getPrimitiveObject(itemName);
                           if(obj instanceof Boolean) {
                              method.invoke(rsp, new Object[]{(Boolean)obj});
                           } else {
                              if(isCheckJsonType && obj != null) {
                                 throw new AlipayApiException(itemName + " is not a Boolean");
                              }

                              if(obj != null) {
                                 method.invoke(rsp, new Object[]{Boolean.valueOf(obj.toString())});
                              }
                           }
                        } else if(Double.class.isAssignableFrom(typeClass)) {
                           obj = reader.getPrimitiveObject(itemName);
                           if(obj instanceof Double) {
                              method.invoke(rsp, new Object[]{(Double)obj});
                           } else if(isCheckJsonType && obj != null) {
                              throw new AlipayApiException(itemName + " is not a Double");
                           }
                        } else if(Number.class.isAssignableFrom(typeClass)) {
                           obj = reader.getPrimitiveObject(itemName);
                           if(obj instanceof Number) {
                              method.invoke(rsp, new Object[]{(Number)obj});
                           } else if(isCheckJsonType && obj != null) {
                              throw new AlipayApiException(itemName + " is not a Number");
                           }
                        } else if(Date.class.isAssignableFrom(typeClass)) {
                           SimpleDateFormat var29 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                           var29.setTimeZone(TimeZone.getTimeZone("GMT+8"));
                           Object paramType = reader.getPrimitiveObject(itemName);
                           if(paramType instanceof String) {
                              method.invoke(rsp, new Object[]{var29.parse(paramType.toString())});
                           }
                        } else if(List.class.isAssignableFrom(typeClass)) {
                           Type var30 = field.getGenericType();
                           if(var30 instanceof ParameterizedType) {
                              ParameterizedType var28 = (ParameterizedType)var30;
                              Type[] genericTypes = var28.getActualTypeArguments();
                              if(genericTypes != null && genericTypes.length > 0 && genericTypes[0] instanceof Class) {
                                 Class subType = (Class)genericTypes[0];
                                 List listObjs = reader.getListObjects(listName, itemName, subType);
                                 if(listObjs != null) {
                                    method.invoke(rsp, new Object[]{listObjs});
                                 }
                              }
                           }
                        } else {
                           obj = reader.getObject(itemName, typeClass);
                           if(obj != null) {
                              method.invoke(rsp, new Object[]{obj});
                           }
                        }
                     }
                  }
               }
            }
         }

         return rsp;
      } catch (Exception var26) {
         throw new AlipayApiException(var26);
      }
   }

   private static Field tryGetFieldWithoutExp(Class clazz, String itemName) {
      try {
         return clazz.getDeclaredField(itemName);
      } catch (Exception var3) {
         return null;
      }
   }

   private static Method tryGetSetMethod(Class clazz, Field field, String methodName) {
      try {
         return clazz.getDeclaredMethod(methodName, new Class[]{field.getType()});
      } catch (Exception var4) {
         return null;
      }
   }

   static {
      baseFields.add("code");
      baseFields.add("msg");
      baseFields.add("subCode");
      baseFields.add("subMsg");
      baseFields.add("body");
      baseFields.add("params");
      baseFields.add("success");
      excludeFields.add("errorCode");
      overideFields.add("code");
      overideFields.add("msg");
   }
}
